import { AxiosInstance, AxiosRequestConfig, AxiosStatic } from './types'
import Axios from './core/Axios'
import { extend } from './helpers/util'
import defaults from './defaults'
import mergeConfig from './core/mergeConfig'
import CancelToken from './cancel/CancelToken'
import Cancel, { isCancel } from './cancel/Cancel'


const createInstance = (config: AxiosRequestConfig): AxiosStatic => {
  let context = new Axios(config)
  let instance = Axios.prototype.request.bind(context)

  extend(instance, context)
  return instance as AxiosStatic

}
// 先创建实例
const axios = createInstance(defaults)

// 在实现创建方法
axios.create = (config: AxiosRequestConfig): AxiosStatic => {
  let axiosRequestConfig = mergeConfig(defaults, config)
  return createInstance(axiosRequestConfig)
}

// 扩展静态方法
axios.CancelToken = CancelToken
axios.Cancel = Cancel
axios.isCancel = isCancel

axios.all = (promises) => {
  return Promise.all(promises)
}

axios.spread = (callback) => {
  return function wrap(arr) {
    return callback.apply(null, arr)
  }
}
axios.Axios = Axios

export default axios
